import React from 'react';
import { Router } from 'dva/router';
import login from './routes/login/login';
import App from './components/home/home';
import CookieUtils from './utils/cookieUtils'

const cached = {};
function registerModel(app, model) {
  if (!cached[model.namespace]) {
    app.model(model);
    cached[model.namespace] = 1;
  }
}
const checkLoginCookie = (nextState, replace) => {
  let login_cookie = CookieUtils.get('login_cookie');
  if (!login_cookie) {
    console.log(`检查login_cookie`);
    replace({
      pathname: '/#/login',
      state: {
        nextPathname: nextState.location.pathname
      }
    })
  }
};
function RouterConfig({ history, app }) {
  const routes = [
    {
      path: '/login',
      name: 'loginPage',
      component: login,
      getIndexRoute(nextState, cb) {}
    }
    // {
    //   path: 'home',
    //   name: 'indexPage',
    //   component: App,
    //   onEnter: checkLoginCookie,
    //   getIndexRoute(nextState, cb) {
    //     registerModel(app, require('./models/home/home'));
    //     registerModel(app, require('./models/list/index'));
    //     require.ensure([], (require) => {
    //       cb(null, {component: require('./routes/list/index')});
    //     });
    //   },
    //   childRoutes:[
    //     {
    //       path: 'add/:id',
    //       name: 'add',
    //       onEnter: checkLoginCookie,
    //       getComponent(nextState, cb) {
    //         require.ensure([], require => {
    //           registerModel(app, require('./models/list/add'));
    //           cb(null, require('./routes/list/add'));
    //         })
    //       }
    //     },
    //     {
    //       path: 'detail/:id',
    //       name: 'detail',
    //       onEnter: checkLoginCookie,
    //       getComponent(nextState, cb) {
    //         require.ensure([], require => {
    //           registerModel(app, require('./models/list/detail'));
    //           cb(null, require('./routes/list/detail'));
    //         })
    //       }
    //     },
    //     {
    //       path: '*',
    //       name: 'error',
    //       onEnter: checkLoginCookie,
    //       getComponent(nextState, cb) {
    //         require.ensure([], require => {
    //           cb(null, require('./routes/error'))
    //         })
    //       }
    //     }
    //   ]
    // }
   ];
  return <Router history={history} routes={routes} />;
}

export default RouterConfig;
